define([
	'../libs/zepto',
    '../common/dialog',
    '../common/ajax',
    '../common/storage',
    '../common/userAction',
    '../common/yingSoftPrg',
    '../common/yingSoftCookie',
],function(){
	var G_ArrNodes = [];
	var G_ArrTitle = [];
	var G_testInfoMenuData = '';
	var beginTime = '';
	var guideId = '';
	var guideTitle = '';
	init();
	function init() {
		//G_UserAction.loadObject();
		//getUserSetting();
		//var setHtml = document.getElementById('settingDialog').innerHTML;
		loadTestInfoMenuJson();
		$('.back').click(function() {
			backClick();
		});
//		G_Prg.$('setting').onclick = function() {
//			G_UserAction.addUserAction('testInfoSettingBtn');
//			settingClick(setHtml);
//		}
	};
	
	function setcpmArrNodes() {
		var _lastMenuID = G_Prg.getQueryString('lastMenuID', true);
		var _ID = _lastMenuID.substring(0, 3);
		var _idxMenu = 1;
		var _menuName;
		var _curMenu = G_testInfoMenuData.Childs;
		var _sonMenu;
		G_ArrTitle.push('重点考点');
		G_ArrNodes.push(_curMenu);
		while(_curMenu) {
			for(var i = 0 && _ID; i < _curMenu.length; i++) {
				if(_curMenu[i].ID !== _ID) {
					continue;
				} else {
					_menuName = _curMenu[i].Name;
					_sonMenu = _curMenu[i].Childs;
					break;
				}
			}
			if(_sonMenu) {
				G_ArrTitle.push(_menuName);
				G_ArrNodes.push(_sonMenu);
			}
			_idxMenu++;
			if(3 * _idxMenu > _lastMenuID.length) {
				break;
			} else {
				_ID = _lastMenuID.substring(0, 3 * _idxMenu);
			}
			_curMenu = _sonMenu;
		}
	}
	
	function loadTestInfoMenuJson() {
		var appEName = G_Cookie.getAppEName();
		console.log(appEName)
		if(!appEName) {
			G_prg.throw('程序运行错误，testInfoMenu.loadTestInfoMenuJson:appEName="' + appEName + '", 获取数据出现异常');
		}
		var _success = function(json) {
			var jsonData = JSON.parse(json);
			if(jsonData.status === 400) {
				G_Prg.throw('程序运行错误：testInfoMenu.loadTestInfoMenuJson,请求参数有误' + jsonData.status, function() {
					window.location.href = 'default.html?fromUrl=html/keyKnowledge.html';
				});
			} else if(jsonData.status === 300) {
				G_Prg.throw('程序运行错误：testInfoMenu.loadTestInfoMenuJson,数据库获取数据异常' + jsonData.status, function() {
					window.location.href = 'default.html?fromUrl=html/keyKnowledge.html';
				});
			} else if(jsonData.status === 201) {
				G_Prg.throw('程序运行错误：testInfoMenu.loadTestInfoMenuJson,暂无备考资料' + jsonData.status, function() {
					window.location.href = 'default.html?fromUrl=html/keyKnowledge.html';
				});
			} else if(jsonData.status === 200) {
				var data = jsonData.data;
				G_testInfoMenuData = JSON.parse(data);
				var fromUrl = G_Prg.getQueryString('fromUrl');
				if(fromUrl === 'userLogin.html' || fromUrl === 'selectPrice.html') {
					setcpmArrNodes();
					createTestInfoMenuHTML(G_ArrNodes[G_ArrNodes.length - 1]);
					var menuName = G_ArrTitle[G_ArrTitle.length - 1];
					if(menuName.length > 12) {
						menuName = menuName.substring(0, 12) + '...';
					}
					G_Prg.$('back').innerHTML = menuName;
					return;
				} else {
					var curMenu = G_testInfoMenuData.Childs;
					console.log(curMenu);
					G_ArrTitle.push('重点考点');
					var tempArr = [];
					for(var i = 0; i < curMenu.length; i++) {
						if(curMenu[i].Name == '重点考点') {
							tempArr.push(curMenu[i]);
						}
					}
					var appEName = G_Cookie.getAppEName();
					var appENameArr = {
						'ZC_FC_YTMJ': true,
						'ZC_GWK_YTMJ': true,
						'ZC_MZ_YTMJ': true,
						'ZC_NK_YTMJ': true,
						'ZC_ZYNK_YTMJ': true,
						'ZC_ZGHS_YTMJ': true,
						'ZC_ZGHSEKHL_YTMJ': true,
						'ZC_ZGHSFKHL_YTMJ': true,
						'ZC_ZGHSSQHL_YTMJ': true,
						'ZC_ZGHSNKHL_YTMJ': true,
						'ZC_ZGHSWKHL_YTMJ': true,
						'ZY_XY_YTMJ': true,
						'ZY_ZYAO_YTMJ': true,
						'ZC_YJSHI_YTMJ': true,
						'ZC_YJS_YTMJ': true,
						'ZC_YS_YTMJ': true,
						'ZY_KQZY_YTMJ': true,
						'ZY_KQZL_YTMJ': true,
						'ZY_GWZY_YTMJ': true,
						'ZY_LCZY_YTMJ': true,
						'ZY_LCZL_YTMJ': true,
						'ZY_ZXYZY_YTMJ': true,
						'ZY_ZXYZL_YTMJ': true,
						'ZY_ZYZY_YTMJ': true,
						'ZY_ZYZL_YTMJ': true
					}
					if(!appEName) {
						G_Prg.alert('请先选择科目。', function() {
							location.href = 'softMenu.html';
						});
						return;
					}
					if(appEName in appENameArr) {
						console.log(appEName)
						$('.title').text('重点考点') ;
						G_Prg.$('SourceTestInfo').innerHTML = '';
						var liTag = document.createElement('li');
						liTag.innerHTML = '重点考点内容将于考前半个月陆续更新<br>敬请期待！';
						liTag.className = 'InternalData';
						G_Prg.$('SourceTestInfo').appendChild(liTag);
						return;
					}
					//console.log(tempArr[0]);
					G_ArrNodes.push(tempArr[0].Childs);
					createTestInfoMenuHTML(tempArr[0].Childs);
					return;
				}
			} else {
				G_maiDian.setBugRecordInfos('其它程序错误', '重点考点——获取目录列表', jsonData.status + ':' + jsonData.msg);
				G_Prg.throw('程序运行错误，不能处理，TestData.getAllTestJSON : status = "' + jsonData.status + '"！', function() {
					window.location.href = '../html/keyKnowledge.html?fromUrl=default.html';
				});
			}
		};
		G_AjaxApi_Record.get('/api/testInfoMenu/getTestInfoMenu/' + appEName, true, _success);
	}
	
	function createTestInfoMenuHTML(curMenu) {
		if(!curMenu) {
			var fromUrl = G_Prg.getQueryString('fromUrl');
			window.location.href = fromUrl;
			return;
		}
		G_Prg.$('menuBar').style.display = 'none';
		G_Prg.$('SourceTestInfo').innerHTML = '';
		window.scrollTo(0, 0);
		addLi(curMenu);
		setCss();
	}
	
	function addLi(curMenu) {
		var appEName = G_Cookie.getAppEName();
		for(var i = 0; i < curMenu.length; i++) {
			var item = curMenu[i];
			var ulTag = document.createElement('ul');
			ulTag.className = 'soft';
			var spanTag = document.createElement('span');
			spanTag.innerHTML = item.Name;
			if((appEName.indexOf('ZC_ZGHS') >= 0 && appEName.indexOf('_YTMJ') >= 0) && item.Name === '重点考点') {
				spanTag.style.color = '#f00';
			}
			var liTag = document.createElement('li');
			liTag.id = i;
			liTag.onclick = function() {
				menuClick(this.id);
			}
			if(item.Childs) {
				liTag.className = 'chapter_fix1';
			} else {
				var testInfoID = G_Cookie.getTestInfoHistory(G_Cookie.getAppEName());
				if(testInfoID) {
					for(var j = 0; j < testInfoID.length; j++) {
						if(testInfoID[j] === item.ID) {
							liTag.className = 'chapter_fix_visited';
							break;
						} else {
							liTag.className = 'chapter_fix3';
						}
					}
				} else {
					liTag.className = 'chapter_fix3';
				}
			}
			G_Prg.$('SourceTestInfo').appendChild(ulTag);
			ulTag.appendChild(liTag);
			liTag.appendChild(spanTag);
		}
	}
	
	function menuClick(curMenuArrIndex) {
		var curMenu = G_ArrNodes[G_ArrNodes.length - 1];
		var menuName = curMenu[curMenuArrIndex].Name;
		var sonMenu = curMenu[curMenuArrIndex].Childs;
		G_UserAction.addUserAction('selGuidMenu_' + G_ArrNodes.length + '_' + menuName);
		var cptID = curMenu[curMenuArrIndex].ID;
		if(!sonMenu) {
			var preMenuName = G_ArrTitle[G_ArrNodes.length - 1];
			var lastMenuID = curMenu[curMenuArrIndex].ID;
			var userID = parseInt(G_Cookie.getUserID());
			var appEName = G_Cookie.getAppEName();
			if((curMenuArrIndex === '0' || curMenuArrIndex === '1' || curMenuArrIndex === '2') && ((preMenuName.replace(/(^\s*)|(\s*$)/g, "") === '考试大纲') || ((lastMenuID.substring(0, 6) === '001001') && (lastMenuID.substring(0, 3) === '001')))) {
				G_ArrTitle.push(menuName);
				G_ArrNodes.push(null);
				G_Cookie.setTestInfoHistory(appEName, curMenu[curMenuArrIndex].ID);
				createTestInfoHtml(appEName, lastMenuID);
			} else {
				if(!userID) {
					var yesCallback = function() {
						window.location.href = 'login?fromUrl=kdzd&toUrl=userLogin.html&lastMenuID=' + lastMenuID;
					}
					if(typeof(G_maiDian) !== 'undefined') {
						//G_maiDian.probationClickCount();
					}
					G_Prg.confirm('请先登录再阅读！', yesCallback);
					return;
				}
				if(!G_Prg.checkVipApp()) {
					var eerorBox = document.getElementById('nrdvMsgBox');
					if(eerorBox && eerorBox.style.display === 'block') {
						return;
					}
					var yesCallback = function() {
						var isiphone = G_Prg.getCookie('CLIENTTYPE');
						if(isiphone && isiphone.toString().toLowerCase() === 'iphone') {
							window.location = "ios://iOSiap";
						} else {
							window.location.href = 'selectPrice.html?fromUrl=keyKnowledge.html&lastMenuID=' + lastMenuID;
						}
					}
					if(typeof(G_maiDian) !== 'undefined') {
						G_maiDian.probationClickCount();
					}
					G_GoBuySoft.goBuySoft('请先购买后再阅读！');
					return;
				}
				G_ArrTitle.push(menuName);
				G_ArrNodes.push(null);
				G_Cookie.setTestInfoHistory(appEName, curMenu[curMenuArrIndex].ID);
				createTestInfoHtml(appEName, lastMenuID);
			}
		} else {
			G_ArrTitle.push(menuName);
			G_ArrNodes.push(sonMenu);
			if(cptID === 'JC_EJJZS2') {
				G_Prg.$('back').innerHTML = menuName;
				G_Prg.$('SourceTestInfo').innerHTML = '';
				var liTag = document.createElement('li');
				liTag.innerHTML = '重点考点内容将于考前半个月陆续更新<br>敬请期待！';
				liTag.className = 'InternalData';
				G_Prg.$('SourceTestInfo').appendChild(liTag);
			} else {
				createTestInfoMenuHTML(sonMenu);
				if(menuName.length > 12) {
					menuName = menuName.substring(0, 12) + '...';
				}
				G_Prg.$('back').innerHTML = menuName;
			}
		}
	}
	
	function createTestInfoHtml(appEName, lastMenuID) {
		var _success = function(testInfoHtml) {
			if(testInfoHtml === '') {
				G_Prg.throw('sqlite数据库没有指南信息', backClick);
				return;
			}
			guideId = lastMenuID;
			beginTime = G_Prg.datetimeFormat(new Date(), 'yyyy-MM-dd hh:mm:ss');
			guideTitle = G_ArrTitle[G_ArrTitle.length - 1];
			var startindex = testInfoHtml.indexOf('<body>') + 6;
			var endindex = testInfoHtml.indexOf('</body>');
			var bodyText = testInfoHtml.substring(startindex, endindex);
			if(bodyText.split('REC-html40\">')[1]) {
				bodyText = bodyText.split('REC-html40\">')[1];
			}
			if(bodyText.split('transitional.dtd">')[1]) {
				bodyText = bodyText.split('transitional.dtd">')[1];
			}
			if(bodyText.split('loose.dtd">')[1]) {
				bodyText = bodyText.split('loose.dtd">')[1];
			}
			bodyText = G_SetTestInfo.setImgTagURL(bodyText);
			bodyText = G_SetTestInfo.setImgURL(bodyText, 'guide');
			var menuName = G_ArrTitle[G_ArrTitle.length - 1];
			if(menuName.length > 12) {
				menuName = menuName.substring(0, 12) + '...';
			}
			G_Prg.$('back').innerHTML = menuName;
			G_Prg.$('SourceTestInfo').innerHTML = bodyText;
			G_Prg.$('SourceTestInfo').className = 'htmlDiv';
			window.scrollTo(0, 0);
			setCss();
			G_Prg.$('menuBar').style.display = 'block';
			return;
		};
		G_AjaxApi_Record.get('/api/testInfoMenu/testInfoHtml?appEName=' + appEName + '&&lastMenuID=' + lastMenuID , true, _success);
	}
	
	function setCss() {
		var sourcrArr = document.getElementsByClassName('htmlDiv');
		if(sourcrArr.length === 0) {
			document.body.style.backgroundColor = '';
			document.body.style.color = '';
			return;
		}
		var fontSize = G_Cookie.getFontSize() || '20px';
		var bgColor = G_Cookie.getDayMode() || G_Cookie.getNightMode() || '#FFF';
		var fontColor = G_Cookie.getDFontColor() || G_Cookie.getNFontColor() || '#000';
		document.body.style.backgroundColor = bgColor;
		document.getElementById('SourceTestInfo').style.fontSize = fontSize;
		document.body.style.color = fontColor;
		var pTag = document.getElementsByTagName('p');
		for(var i = 0; i < pTag.length; i++) {
			pTag[i].style.fontSize = fontSize;
			pTag[i].style.color = fontColor;
			pTag[i].style.lineHeight = '135%';
		}
		var tableTag = document.getElementsByTagName('table');
		for(var i = 0; i < tableTag.length; i++) {
			tableTag[i].style.borderColor = fontColor;
		}
		var imgTag = document.body.getElementsByTagName('img');
		for(var i = 0; i < imgTag.length; i++) {
			imgTag[i].style.maxWidth = '100%';
			imgTag[i].style.height = 'auto';
			if(imgTag[i].src.indexOf('http://t.api.ksbao.com/', 0) > -1) {
				imgTag[i].onclick = function() {
					var b = this;
					G_ImageZoom(b);
				};
			}
		}
		var spanTag = document.getElementsByTagName('span');
		for(var i = 0; i < spanTag.length; i++) {
			spanTag[i].style.fontSize = fontSize;
			spanTag[i].style.color = fontColor;
			spanTag[i].style.lineHeight = '135%';
		}
		var fontTag = document.getElementsByTagName('font');
		for(var i = 0; i < fontTag.length; i++) {
			fontTag[i].style.fontSize = fontSize;
			fontTag[i].style.color = fontColor;
			fontTag[i].style.lineHeight = '135%';
		}
		var tdTag = document.getElementsByTagName('td');
		for(var i = 0; i < tdTag.length; i++) {
			tdTag[i].style.fontSize = fontSize;
			tdTag[i].style.color = fontColor;
			tdTag[i].style.lineHeight = '135%';
		}
	}
	
	function backClick() {
		window.location.href=''
		if(beginTime) {
			var endTime = G_Prg.datetimeFormat(new Date(), 'yyyy-MM-dd hh:mm:ss');
			if(typeof(G_maiDian) !== 'undefined') {
				G_maiDian.guideClickCount(guideId, beginTime, endTime, guideTitle);
			}
		}
		beginTime = '';
		if(G_ArrNodes.length === 1 || G_ArrNodes.length === 0) {
			window.location.href = '/doExam-index?fromUrl=kdzd'
//			var fromUrl = G_Prg.getQueryString('fromUrl');
//			if(!fromUrl || fromUrl === 'userLogin.html' || fromUrl === 'selectPrice.html') {
//				var fromUrl = '../html/default.html?fromUrl=keyKnowledge.html';
//				window.location.href = fromUrl;
//			} else {
//				window.location.href = fromUrl + '?fromUrl=keyKnowledge.html';
//			}
			return;
		}
		G_ArrNodes.pop();
		G_ArrTitle.pop();
		var curMenu = G_ArrNodes[G_ArrNodes.length - 1];
		G_Prg.$('SourceTestInfo').className = 'chapter_fix';
		createTestInfoMenuHTML(curMenu);
		var menuName = G_ArrTitle[G_ArrTitle.length - 1];
		if(menuName.length > 12) {
			menuName = menuName.substring(0, 12) + '...';
		}
		G_Prg.$('back').innerHTML = menuName;
	}
	
});